package notice

import (
	"gddgame.cc/galaxy/binocle"
	"gddgame.cc/galaxy/binocle/plugins/wechat"
	. "gddgame.cc/galaxy/common/dsl/db"
	. "gddgame.cc/galaxy/common/dsl/web"
	"gddgame.cc/galaxy/server/composer"
)

const PluginName = "notice"

//var (
//	currentClient *cluster.Client
//)

func init() {
	RegisterPlugin(PluginProvider{
		Name:    PluginName,
		Depends: []string{wechat.PluginName},

		OnInit: func(builder composer.Builder, core binocle.CoreClient) error {
			// 绑定通知接口
			//fmt.Println("bind")
			core.BindNoticeHandler(func(publish string, data map[string]interface{}) {
				//fmt.Println(publish, data)
				_ = SendNotice(builder, publish, data)
			})
			// MountClient 会丢失部分发送通知，无client依赖，直接进行绑定
			//system.MountClient(func(client *cluster.Client) {
			//	currentClient = client
			//	MountClient(builder, client)
			//})

			AddTable(&Table{
				Model: NoticeModel,
			})
			return nil
		},
		BindRouter: BindRouter,
	})
}

func BindRouter(builder composer.Builder, core binocle.CoreClient, r *SwaggerRouter) {
	// wechat notice setting
	r.Put("/notice/send/wechat/{key}", Swagger{
		Controller: wechat.PushWechat,
		Params: []Param{
			{Name: `key`, In: Path},
			{Name: `message`, In: Body, Required: true},
		},
		Response: ApiResponse,
	})
}
